﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Audio;

namespace Example1.View
{
    class View : Model.IObserver
    {
        private SpriteBatch     m_spriteBatch;
        private Texture2D       m_texture;
        private KeyboardState   m_oldKeyboardState;
        private float           m_viewScale = 64;
        private SoundEffect     m_jumpSound;

        public View(GraphicsDeviceManager a_manager, ContentManager a_contentLoader)
        {
            //Save start key state to detect changes
            m_oldKeyboardState = Keyboard.GetState();

            //this is needed to draw sprites
            m_spriteBatch = new SpriteBatch(a_manager.GraphicsDevice);

            //Load content
            m_texture = a_contentLoader.Load<Texture2D>("player");
            m_jumpSound = a_contentLoader.Load<SoundEffect>("playerJump");
        }

        internal void DrawLevel(Microsoft.Xna.Framework.Graphics.GraphicsDevice a_graphicsDevice, Microsoft.Xna.Framework.Vector2 a_modelPlayerPosition)
        {
            //draw green background
            a_graphicsDevice.Clear(Microsoft.Xna.Framework.Color.Green);

            //draw all images
            m_spriteBatch.Begin();
            DrawPlayerAt(a_graphicsDevice, a_modelPlayerPosition);
            m_spriteBatch.End();
        }

        private void DrawPlayerAt(Microsoft.Xna.Framework.Graphics.GraphicsDevice a_graphicsDevice, Microsoft.Xna.Framework.Vector2 a_modelPlayerPosition)
        {
            
            //Note that translating from model to view positions can be confusing...
            //a_modelPlayerPosition is on the bottom left corner of the sprite...
            Vector2 scaledPosition = a_modelPlayerPosition * m_viewScale;
            int viewPlayerPositionX = (int)scaledPosition.X;

            //turn position upside down since upper window left corner is 0,0 in view space
            //and 0,0 in model space is lower left corner
            int viewPlayerBottomPositionY = a_graphicsDevice.Viewport.Height - (int)scaledPosition.Y;

            int spriteHeight = (int)m_viewScale;
            int spriteWidth = (int)m_viewScale;

            //the model position is on the bottom of the player, but texture rectangles are drawn from top left.
            int viewPlayerTopPositionY = viewPlayerBottomPositionY - (int)spriteHeight;

            //Create rectangle and draw it
            Rectangle destRect = new Rectangle(viewPlayerPositionX, viewPlayerTopPositionY, spriteWidth, spriteHeight);
            m_spriteBatch.Draw(m_texture, destRect, Color.White);
        }

        internal bool DidPlayerPressJump()
        {
            Keys jumpKey = Keys.Space; 
            bool ret = false;

            KeyboardState newState = Keyboard.GetState();

            //has been pressed and released
            if (m_oldKeyboardState.IsKeyDown(jumpKey) && newState.IsKeyUp(jumpKey))
            {
                ret = true;
            }
            //save state
            m_oldKeyboardState = newState;

            //return true if release of key was detected
            return ret;
        }

        internal void DoJump()
        {
            m_jumpSound.Play();
        }

        public void LandOnFloor()
        {
            m_jumpSound.Play();
        }
    }
}
